package it.polimi.traveldream.ejb.bean;



import it.polimi.authentication.ejb.usermanagement.AddNewCity;
import it.polimi.authentication.ejb.usermanagement.AddNewHotel;
import it.polimi.authentication.ejb.usermanagement.Hotel;
import it.polimi.authentication.ejb.usermanagement.Pacchetto;
import it.polimi.authentication.ejb.usermanagement.Citta;
import it.polimi.authentication.ejb.usermanagement.PacchettoCitta;
import it.polimi.authentication.ejb.usermanagement.PacchettoPredefinito;
import it.polimi.authentication.ejb.usermanagement.User;
import it.polimi.authentication.ejb.usermanagement.dto.CittaDTO;
import it.polimi.authentication.ejb.usermanagement.dto.HotelDTO;
import it.polimi.authentication.ejb.usermanagement.dto.PacchettoPredefinitoDTO;
import it.polimi.authentication.ejb.usermanagement.dto.Pacchetto_cittaDTO;

import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class UserBean
 */
@Stateless
public class AggiungiNuovoHotel implements AddNewHotel{


	@PersistenceContext
    private EntityManager em;

	
	public AggiungiNuovoHotel(){	
	}
	
	@Override
	public boolean add(HotelDTO hotel)
	{
		ArrayList <Hotel> hotelApp = (ArrayList <Hotel>) em.createNamedQuery("Hotel.findThis").setParameter("nomeH", hotel.getNome()).getResultList();
		
		if (hotelApp.size() ==0)
		{
			//crea nuovo hotel
			Hotel newH = new Hotel (hotel);
			newH.setDisponibile(true);
			//inserisci 	
			em.persist(newH);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	@Override
	public void delete (ArrayList<String> DelHotel)
	{
		System.out.println("arrivo");
		for (int i=0; i< DelHotel.size(); i++)
		{
			Query query = em.createQuery ("UPDATE Hotel h SET h.Disponibile = 0 WHERE h.nomeHotel = '"+ DelHotel.get(i)+"'");
			int updateCount = query.executeUpdate();
		}
	}
	
	
}


